##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################



proc get_client_invoices {w} {
	set cli_name [$w get]
	if {$cli_name ne ""} {
		$::daidze_main state_widget {invoice} readonly
		set inv_list [$::db select_all invoice_vs_aggregated_payment invoice_id "client_name=\'$cli_name\' AND debt>0" list]
		$::daidze_main new_form_values [dict create invoice $inv_list]
	} else {
		$::daidze_main state_widget {invoice} disabled 1
	}
}

proc add_payment {} {
	set values [$::daidze_main get_values]
	set msg_list [dict create fk_client "Client name is obligatory." pay_date "Date field is obligatory." pay_time "Time field is obligatory." amount "Amount field is obligatory." invoice "Invoice field is obligatory." fk_payment_type "Payment type is obligatory."]
	set flags [list]

	foreach key_val [dict keys $msg_list] {

		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 
	
	if {[lsearch -not -exact $flags 1] == "-1"} {
		set inv_id [dict get $values invoice]
		set inv_total_dict [$::db select_all invoice_vs_aggregated_payment debt invoice_id=\'$inv_id\']
		foreach d [dict values $inv_total_dict] {
			set inv_total [lindex $d 1]
		}
		if {[dict get $values amount] > $inv_total} {
			tk_messageBox -icon warning -type ok -message "[::msgcat::mc "Amount of payment bigger than amount of invoice and will be reduced to invoice total. \n Give the change please !"]"
			dict set values amount $inv_total
		}
		$::db insert_all payment $values
		$::daidze_main load_main payment payment.xml $::main 1 
	}
}

proc print_payment {table_var} {
	set print_data [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]

	# Get company data
	set company_data [$::db select_all company {name tel fin address fax email} {def='1'} list]
	foreach company_key {company_name company_tel company_fin company_address company_fax company_email} val $company_data {
		dict set print_data $company_key $val
	}

	set vars {id fk_client pay_date amount invoice fk_payment_type}
	set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
	foreach key $vars value $row {
		dict set print_data $key $value
	}
	dict set print_data fin [$::db execute_query "SELECT fin FROM client WHERE name='[dict get $print_data fk_client]'"]
	set pdf [my_print new [file join $::models receipt_general.xml] $print_data]
}